// pages/yuanzhangemaildetail/yuanzhangemaildetail.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    message:"",
    contentid:null,
    posterType:null,
    contentlist:null,
    replayList:[],
    show: true,
    commentValue:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var message = JSON.parse(options.message)
    console.log(message)
    this.setData({
      contentid:message.mailID,
      posterType:message.senderType
    })
    this.getInfo('正在加载数据...')
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getInfo('正在加载数据...')
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // 获取分页列表
  getInfo: function (message) {
    var that = this;
    wx.showNavigationBarLoading() //在当前页面显示导航条加载动画
    wx.showLoading({ //显示 loading 提示框
      title: message,
    })
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "contentid": that.data.contentid,
      "posterType": that.data.posterType,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "mobileSerialnumber": ""
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "get_email_detail"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 0) {

          wx.hideNavigationBarLoading() //在当前页面隐藏导航条加载动画
          wx.hideLoading() //隐藏 loading 提示框
          var contentlist = res.data.data;
          try {
            contentlist.contents = decodeURIComponent(contentlist.contents)
          } catch (error) {
            // 此处是负责例外处理的语句
            contentlist.contents = contentlist.contents
          } finally {
            // 此处是出口语句
          }

          var list=contentlist.replayList
          list.forEach(function (item, index) {
            console.log(item); //这里的item就是从数组里拿出来的每一个每一组
            try {
              list[index].contents = decodeURIComponent(item.contents)
            } catch (error) {
              // 此处是负责例外处理的语句
              list[index].contents = item.contents
            } finally {
              // 此处是出口语句
            }
          })

          that.setData({
            contentlist:contentlist,
            replayList:list
          })

            
        } else {
          wx.hideNavigationBarLoading()
          wx.hideLoading()
          wx.showToast({
            title: "获取数据失败",
            icon: 'none',
            duration: 1000,
            mask: true
          })
        }
      },
      fail: function (res) {
        wx.hideNavigationBarLoading()
        wx.hideLoading()
      },
      complete: function (res) {
        console.log('submit complete');
      }
    })

  },
  sendComment: function (e) {
    if (this.data.commentValue == null || this.data.commentValue.length == 0) {
      wx.showToast({
        title: "请输入评论内容",
        icon: 'none',
        duration: 1000,
        mask: true
      })
    } else {
      this.commentPost()
      
    }
  },

  getInput: function (e) {
    this.setData({
      commentValue: e.detail.value
    })
  },

  commentPost: function () {
    var that = this;
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "parentID": this.data.contentid,
      "content": this.data.commentValue,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "mobileType": app.globalData.mobileType,
      "timeSpan": app.globalData.timespan,
      "mobileSerialnumber": ""
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "send_email_reply"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 0) {
          wx.showToast({
            title: "回复成功",
            icon: 'none',
            duration: 1000,
            mask: true
          })
          that.setData({
            show: true,
            commentValue:""
          });
          that.getInfo('正在刷新数据')
        } else {
          wx.showToast({
            title: "回复失败",
            icon: 'none',
            duration: 1000,
            mask: true
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: "回复失败",
          icon: 'none',
          duration: 1000,
          mask: true
        })
      },
      complete: function (res) {
        console.log('submit complete');
      }
    })
  },

})